/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.cas.support.user.userregistry.web;
 
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;  

import org.qloudgen.sec.cas.support.user.services.User;
import org.qloudgen.sec.cas.support.user.services.UsersManager;

import java.util.List;  
import java.util.Map;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ManageUserMultiActionController extends MultiActionController{  
	private static final Logger logger = LoggerFactory.getLogger(ManageUserMultiActionController.class);
  
    /** View name for User View. */
    private static final String VIEW_NAME_MANAGE = "manageUserView";

    private UsersManager usersManager;  
    
    public ManageUserMultiActionController(final UsersManager usersManager){
    	  super();
    	  this.usersManager = usersManager;
    }
    
    public UsersManager getUsersManager() {  
        return usersManager;  
    }  
  
    public void setUsersManager(UsersManager usersManager) {  
        this.usersManager = usersManager;  
    }  
  

    public ModelAndView manageUser(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {  
        // TODO Auto-generated method stub  
        
        final Map<String, Object> model = new HashMap<String, Object>();
        
        final List<User> list = this.usersManager.getAllUsers();
        
        logger.debug("**********************************");
        logger.debug("user count:"+list.size());
        logger.debug("**********************************");
        
        model.put("users", list);
        model.put("pageTitle", VIEW_NAME_MANAGE);
        
        return new ModelAndView(VIEW_NAME_MANAGE, model);  
    }     
    
    
    public ModelAndView deleteUser(final HttpServletRequest request, 
            final HttpServletResponse response) throws Exception {
        logger.debug("start deleteUser");
        final String id = request.getParameter("id");
        
        final ModelAndView modelAndView = new ModelAndView(new RedirectView(
                "manageUser.html", true), "status", "deleted");
       
        final User r = this.usersManager.delete(id);
        
         modelAndView.addObject("name", r != null
                ? r.getName() : "");
        logger.debug("end deleteUser");
        return modelAndView;
    }
} 